//
//  FlyCollectionView.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2017/9/7.
//  Copyright © 2017年 lifayu. All rights reserved.
//

#import "FlyCollectionView.h"

@interface FlyCollectionView()<FlyCollectionIndexViewDelegate>
{
    UILabel *_flotageLabel;//中间显示的背景框
    
}
@end

@implementation FlyCollectionView


- (id)init
{
    return [self initWithFrame:CGRectZero];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        [self _setupSubviews];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self)
    {
        [self _setupSubviews];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _collectionView.frame = self.bounds;
    _flotageLabel.frame = (CGRect){(self.bounds.size.width - 64 ) / 2,(self.bounds.size.height - 64) / 2,64,64};
}

- (void)_setupSubviews
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.backgroundColor = kClearColor;
    [self addSubview:_collectionView];
    
    _collectionIndexView = [[FlyCollectionIndexView alloc] init];
    _collectionIndexView.indexDelegate = self;
    _collectionIndexView.backgroundColor = kClearColor;
    [self addSubview:_collectionIndexView];
    
    _flotageLabel = [[UILabel alloc] init];
    _flotageLabel.backgroundColor = [UIColor blackColor];
    _flotageLabel.hidden = YES;
    [_flotageLabel.layer setCornerRadius:32];
    _flotageLabel.layer.masksToBounds = YES;
    _flotageLabel.textAlignment = NSTextAlignmentCenter;
    _flotageLabel.textColor = [UIColor whiteColor];
    [self addSubview:_flotageLabel];
}

- (void)setDelegate:(id<FlyCollectionViewDelegate>)delegate
{
    _delegate = delegate;
    
    _collectionView.delegate = delegate;
    _collectionView.dataSource = delegate;
    
    _collectionIndexView.indexs = [self.delegate indexTitlesForFlyCollectionView:self];
}

- (void)reloadData
{
    _collectionIndexView.indexs = [self.delegate indexTitlesForFlyCollectionView:self];
    
    CGFloat collectionIndexViewHeight = FlyCollectionViewIndexHeight*_collectionIndexView.indexs.count;
    CGFloat collectionIndexViewY = self.bounds.size.height/2 - collectionIndexViewHeight/2;
    _collectionIndexView.frame = (CGRect){self.bounds.size.width - 20, collectionIndexViewY, 20,collectionIndexViewHeight};
    
    [_collectionView reloadData];
}

#pragma mark- DSCollectionViewIndexDelegate

-(void)collectionIndexView:(FlyCollectionIndexView *)collectionIndexView didselectionAtIndex:(NSInteger)index title:(NSString *)title
{
    
    if ([_collectionView numberOfSections]>index&&index>-1)
    {
        [_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:index] atScrollPosition:UICollectionViewScrollPositionTop animated:YES];
        _flotageLabel.text = title;
    }
}

-(void)collectionIndexViewTouchesBegan:(FlyCollectionIndexView *)collectionIndexView
{
    _flotageLabel.alpha = 1;
    _flotageLabel.hidden = NO;
}

-(void)collectionIndexViewTouchesEnd:(FlyCollectionIndexView *)collectionIndexView
{
    
    void (^animation)() = ^{
        _flotageLabel.alpha = 0;
    };
    
    [UIView animateWithDuration:0.4 animations:animation completion:^(BOOL finished)
     {
         _flotageLabel.hidden = YES;
     }];
}



@end
